﻿using AutoMapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using NetTaste;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq.Expressions;
using System.Numerics;
using System.Reflection;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text.RegularExpressions;
using VM.BLL.Menu;
using VM.Common.DataUntil;
using VM.Common.EntityEnum;
using VM.Common.Result;
using VM.DAL.System.Code;
using VM.Entity.EntityDto.Order;
using VM.Entity.EntityDto.ShopCar;
using VM.Entity.EntityMap.DataBase;
using VM.Entity.EntityMap.Goods;
using VM.Entity.EntityMap.ICon;
using VM.Entity.EntityMap.Lunbo;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.MsgCode;
using VM.Entity.EntityMap.Order;
using VM.Entity.EntityMap.Region;
using VM.Entity.EntityMap.ShopCar;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Token;
using VM.Entity.EntityMap.Topic;
using VM.Entity.EntityMap.User;
using VM.IBLL.IOrder;
using VM.IBLL.IShopCar;
using VM.IBLL.Member;
using VM.IBLL.Menu;
using VM.IBLL.System.Code;
using VM.IBLL.Token;
using VM.IBLL.Topic;
using VM.IDAL;
using VM.IDAL.ShopCar;


using static Dm.net.buffer.ByteArrayBuffer;

namespace VM.BLL.Order
{
    public class OrderBLL : BaseBLL, IOrderBLL
    {

        private readonly ISqlSuggarDAL _ISqlSuggarDAL;
        private readonly IOrderDAL _IOrderDAL;
        private readonly IMapper _IMapper;
        private readonly ICodeHelperBLL _ICodeHelperBLL;

        public OrderBLL(ISqlSugarClient client, ISqlSuggarDAL sqlSuggarDAL, IMenuBLL iMenuBLL, IOrderDAL iOrderDAL, IMapper iMapper, ICodeHelperBLL iCodeHelperBLL)
        {
            _ISqlSuggarDAL = sqlSuggarDAL;
            _IOrderDAL = iOrderDAL;
            _IMapper = iMapper;
            _ICodeHelperBLL = iCodeHelperBLL;

        }
        /// <summary>
        /// 批量添加订单
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public ApiDataResult<List<getOrderMainInfoDto>> addOrder(addMultyOrderDto obj,int UserId)
        {
            ApiDataResult<List<getOrderMainInfoDto>> res = new ApiDataResult<List<getOrderMainInfoDto>>();
            List<getOrderMainInfoDto> ListRes = new List<getOrderMainInfoDto>();
            if (obj.orderInfo.Count > 0)
            {
                int SuccessCount = 0;
                for (int i = 0; i < obj.orderInfo.Count; i++)
                {
                  
                    sys_order objRes = _IMapper.Map<addOrderDto, sys_order>(obj.orderInfo[i]);
                    objRes.createUserId = UserId;
                    objRes.createDate = (DateTime)obj.createDate;
                    objRes.updateUserId = UserId;
                    objRes.updateDate = (DateTime)obj.createDate;
                    objRes.OrderNo = UserId.ToString() + RandomNo.GetRandomNo6().ToString() + Timestamp.getNowTimeStamp().ToString() ;
                    objRes.status = 1;
                    sys_order Res = _IOrderDAL.addOrder(objRes);

                   
                    if (Res.id>0)
                    {
                        SuccessCount++;
                        getOrderMainInfoDto objTemp = _IMapper.Map<sys_order, getOrderMainInfoDto>(Res);
                        ListRes.Add(objTemp);
                    }
                   
                }
                if (SuccessCount > 0)
                {
                    res.code = 200;
                    res.msg = $@"成功插入{SuccessCount}条订单";
                    res.data = ListRes;
                }
                else
                {
                    res.code = 403;
                    res.msg = $@"插入失败";
                    res.data = null;
                }               
            }
            else
            {

                res.code = 403;
                res.msg = $@"分录无数据，请检查";
                res.data = null;
            }
            return res; 
        }

         
          /// <summary>
          /// 获取我的订单列表，分页数据
          /// </summary>
          /// <param name="userId"></param>
          /// <param name="pageIndex"></param>
          /// <param name="pageSize"></param>
          /// <returns></returns>
          
        public ApiDataResult<PagingData<getOrderListDto>> getOrderList(int userId, int status, string OrderInfo, int pageIndex, int pageSize)
        {
            PagingData<getOrderListDto> List = _IOrderDAL.getOrderList(userId,   status,   OrderInfo, pageIndex, pageSize);
            ApiDataResult<PagingData<getOrderListDto>> res = new ApiDataResult<PagingData<getOrderListDto>>()
            {

                code = 200,
                msg = "获取成功",
                data = List
            };
            return res;
        }

        public ApiResult deleteOrderById(int userId, int shopCarId)
        {
            int  Res = _IOrderDAL.deleteOrderById(userId, shopCarId);
            if (Res>0)
            {
                ApiResult res = new ApiResult()
                {
                    code = 200,
                    msg = "删除成功"

                };
                return res;

            }
            else
            {

                ApiResult res = new ApiResult()
                {
                    code = 403,
                    msg = "删除失败"

                };
                return res;
            }
        }



        public ApiDataResult<getOrderInfoDto> getOrderInfoById(int id)
        {
            getOrderInfoDto  Res = _IOrderDAL.getOrderInfoById(id);
            ApiDataResult<getOrderInfoDto> res = new ApiDataResult<getOrderInfoDto>()
            {

                code = 200,
                msg = "获取成功",
                data = Res
            };
            return res;
        }

        public ApiResult cancelOrderById(int id)
        {
            int Res = _IOrderDAL.cancelOrderById( id);
            if (Res > 0)
            {
                ApiResult res = new ApiResult()
                {
                    code = 201,
                    msg = "取消成功"

                };
                return res;

            }
            else
            {

                ApiResult res = new ApiResult()
                {
                    code = 403,
                    msg = "取消失败"

                };
                return res;
            }
        }
        /// <summary>
        /// 更新订单状态
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ApiResult updateOrderStatusByIds(List<updateOrderStatusByIdDto> obj, int userId)
        {
            int UpCount = 0;
            for (int i = 0; i < obj.Count; i++)
            {
                updateOrderStatusByIdDto item= obj[i];
                item = _ICodeHelperBLL.perfectClass<updateOrderStatusByIdDto>(item, userId, ProTypeEnum.save);
                int Res = _IOrderDAL.updateOrderStatusById(item);
                if (Res > 0) {
                    UpCount++; 
                } 
            }
            if (UpCount > 0)
            {
                ApiResult res = new ApiResult()
                {
                    code = 201,
                    msg = $@"成功更新{UpCount}条订单数据"

                };
                return res; 
            }
            else
            { 
                ApiResult res = new ApiResult()
                {
                    code = 403,
                    msg = "无数据更新" 
                };
                return res;
            } 
        }

        public ApiResult updateOrderQty(List<updateStockQtyDto> obj)
        {
            //先检查下库存是否合适
            for (int i = 0; i < obj.Count; i++)
            {
                int CheckRes = _IOrderDAL.checkStockQty(obj[i].specId, obj[i].qty);
                if (CheckRes > 0)
                { 
                }
                else
                {
 
                    ApiResult reserro = new ApiResult()
                    {
                        code = 403,
                        msg = $@"第{i+1}个商品库存不足"
                    };
                    return reserro;
                }

            }
            int UpdateCount = 0;
            for (int j = 0; j < obj.Count; j++)
            { 
            int Res = _IOrderDAL.updateOrderQty(obj[j].specId, obj[j].qty);
            if (Res > 0)
            {
                    UpdateCount++;
            }
            else
            {

                ApiResult resErro = new ApiResult()
                {
                    code = 403,
                    msg = $@"更新失败"

                };
                return resErro;
            }

            }
            ApiResult res = new ApiResult()
            {
                code = 201,
                msg = $@"批量更新成功{UpdateCount}条库存数据"

            };
            return res;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="specId"></param>
        /// <param name="qty"></param>
        /// <returns></returns>
        public ApiResult checkStockQty(List<updateStockQtyDto> obj)
        {
            for (int i = 0; i < obj.Count; i++)
            { 
            int Res = _IOrderDAL.checkStockQty(obj[i].specId, obj[i].qty);
            if (Res >= 0)
            {
            }
            else
            {
                ApiResult resErro = new ApiResult()
                {
                    code = 403,
                    msg = $@"第{i+1}个商品库存不足" 
                };
                return resErro;
            }
            }
            ApiResult res = new ApiResult()
            {
                code = 200,
                msg = $@"所有商品库存足够"
            };
            return res;

        }
    }
}




